package com.ljx.offer;

import java.sql.Array;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 李捷禧
 * Date: 2023/3/29
 * ClassName: C_32_1
 */

public class C_32_1 {
    List<List<Integer>> lists;

    /**
     * 模仿32题的遍历整个数的节点
     * @param root
     * @return
     */
    public int[] levelOrder(TreeNode root) {
        lists = new ArrayList<>();
        deep(0,root);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i <lists.size() ; i++) {
            list.addAll(lists.get(i));
        }
        int[] arr = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);
        }
        return arr;
    }
    void deep(int depth,TreeNode root){
        if(root == null) return;
        if (depth == lists.size()) lists.add(new LinkedList<>());
        lists.get(depth).add(root.val);
        deep(depth + 1,root.left);
        deep(depth + 1,root.right);
    }

    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }
}
